using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Service.WxSub.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application.Service.WxSub;

/// <summary>
/// 微信子商户配置
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class WxPaySubService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<WxPaySub> _rep;

	public WxPaySubService(SqlSugarRepository<WxPaySub> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 获取所有租户的字典
	/// </summary>
	/// <returns></returns>
	[HttpGet("/wxPaySub/getsubTenant")]
	public async Task<dynamic> GetsubTenant()
	{
		return await _rep.Context.Queryable<SysTenant>().LeftJoin((SysTenant t, SysUser u) => t.UserId == u.Id).LeftJoin((SysTenant t, SysUser u, SysOrg o) => t.OrgId == o.Id)
			.Select((SysTenant t, SysUser u, SysOrg o) => new SubDic
			{
				id = t.Id,
				name = o.Name
			})
			.ToListAsync();
	}

	/// <summary>
	/// 分页查询子商户号
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/wxPaySub/page")]
	public async Task<dynamic> Page([FromQuery] SubPageInput input)
	{
		ISugarQueryable<WxPaySub> query = _rep.Context.Queryable<WxPaySub>().WhereIF(!string.IsNullOrWhiteSpace(input.SubName), (WxPaySub u) => u.Remark.Contains(input.SubName.Trim()));
		query = query.OrderBuilder(input);
		SqlSugarPagedList<SubPageOutPut> list = await query.Select((WxPaySub s) => new SubPageOutPut
		{
			Id = s.Id,
			SubAppId = s.SubAppId,
			SubAppSecret = s.SubAppSecret,
			SubMerchantId = s.SubMerchantId,
			CreateTime = s.CreateTime,
			Remark = s.Remark,
			SubName = s.SubName,
			SubTenantId = s.SubTenantId
		}).ToPagedListAsync(input.Page, input.PageSize);
		List<SubPageOutPut> listItem = list.Items.ToList();
		string pattern = "(?<=[\\S]{4})\\S(?=[\\S]{4})";
		Regex rgx = new Regex(pattern);
		listItem.ForEach(delegate(SubPageOutPut u)
		{
			if (u.SubMerchantId != null && u.SubMerchantId.Length > 8)
			{
				u.SubMerchantId = rgx.Replace(u.SubMerchantId, "*");
			}
			if (u.SubAppId != null && u.SubAppId.Length > 8)
			{
				u.SubAppId = rgx.Replace(u.SubAppId, "*");
			}
			if (u.SubAppSecret != null && u.SubAppSecret.Length > 8)
			{
				u.SubAppSecret = rgx.Replace(u.SubAppSecret, "*");
			}
		});
		return list;
	}

	/// <summary>
	/// 增加子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/wxPaySub/add")]
	public async Task Add(AddSubInput input)
	{
		WxPaySub entity = input.Adapt<WxPaySub>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/wxPaySub/delete")]
	public async Task Delete(DeleteSubInput input)
	{
		WxPaySub entity = await _rep.GetFirstAsync((WxPaySub u) => u.Id == input.Id);
		if (entity != null)
		{
			await _rep.DeleteAsync(entity);
		}
	}

	/// <summary>
	/// 更新子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/wxPaySub/edit")]
	public async Task Update(UpdateSubInput input)
	{
		WxPaySub entity = input.Adapt<WxPaySub>();
		WxPaySub model = await _rep.GetFirstAsync((WxPaySub u) => u.Id == input.Id);
		if (model != null)
		{
			if (entity.SubMerchantId != null && !entity.SubMerchantId.Contains("*"))
			{
				model.SubMerchantId = entity.SubMerchantId;
			}
			if (entity.SubAppId != null && !entity.SubAppId.Contains("*"))
			{
				model.SubAppId = entity.SubAppId;
			}
			if (entity.SubAppSecret != null && !entity.SubAppSecret.Contains("*"))
			{
				model.SubAppSecret = entity.SubAppSecret;
			}
			model.Remark = entity.Remark;
			model.SubName = entity.SubName;
			model.SubTenantId = entity.SubTenantId;
			await _rep.UpdateAsync(model);
			return;
		}
		throw Oops.Bah("当前数据不存在");
	}
}
